home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / libdemo / percentDone.c++ < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  134 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <libc.h>
  18. #include <gl.h>
  19. #include <task.h>
  20. #include <device.h>
  21.  
  22. #include "event.h"
  23. #include "percentDone.h"
  24.  
  25. extern "C" int sginap(long);
  26.  
  27.  
  28. percentDone::percentDone(char * t, float (* f)(void *), void * d)
  29. {
  30.     title = t;
  31.     func = f;
  32.     data = d;
  33.  
  34.     fatalError = 0;
  35.     howFar = 0;
  36.     howFarDisplayed = -1;
  37.     doUpdate = 1;
  38. }
  39.  
  40. percentDone::~percentDone()
  41. {
  42.     delete_events(ANY, REDRAW, (int)window_id, (void *)ANY);
  43.     delete_updates(&doUpdate, (void *)ANY);
  44.  
  45.     winclose(window_id);
  46. }
  47.  
  48. void
  49. percentDone::start()
  50. {
  51.     prefposition(getgdesc(GD_XPMAX) / 2 - 50,
  52.          getgdesc(GD_XPMAX) / 2 + 50,
  53.          getgdesc(GD_YPMAX) / 2 - 10,
  54.          getgdesc(GD_YPMAX) / 2 + 10);
  55.     foreground();
  56.     window_id = winopen(title);
  57.     ortho2(0, 1, 0, 1);
  58.     update();
  59.     
  60.     add_event(ANY, REDRAW, (int)window_id, 
  61.         (void (*)())percentDone::redraw, this);
  62.     add_update(&doUpdate, (void (*)())percentDone::update, this);
  63.  
  64.     task_id = taskcreate("percentDone", 
  65. #ifdef _SYSTYPE_SVR4
  66.              (void(*)(void *))percentDone::doIt,
  67. #else
  68.              (void(*)())percentDone::doIt,
  69. #endif
  70.              this, 0);
  71. }
  72.  
  73.  
  74. int
  75. percentDone::isDone()
  76. {
  77.     if (fatalError)
  78.     exit(1);
  79.  
  80.     return (howFar ==  1.0);
  81. }
  82.  
  83.  
  84. void
  85. percentDone::wait()
  86. {
  87.     while (! isDone())
  88.     event();
  89. }
  90.  
  91.  
  92. void 
  93. percentDone::doIt()
  94. {
  95.     do {
  96.     howFar = (*func)(data);
  97.     if (howFar == -1) {
  98.         fatalError = 1;
  99.         taskdestroy((int)task_id);
  100.     }
  101.     } while (howFar != 1.0);
  102.  
  103.     taskdestroy((int)task_id);
  104. }
  105.  
  106.  
  107. void
  108. percentDone::update()
  109. {
  110.     if (howFarDisplayed != howFar) {
  111.     winset(window_id);
  112.  
  113.     color(14);
  114.     rectf(0, 0, howFar, 1.0);
  115.     color(7);
  116.     rectf(howFar, 0.0, 1.0, 1.0);
  117.  
  118.     howFarDisplayed = howFar;
  119.     }
  120.     else
  121.     sginap(33);
  122. }
  123.  
  124.  
  125. void
  126. percentDone::redraw()
  127. {
  128.     winset(window_id);
  129.     reshapeviewport();
  130.     howFarDisplayed = -1;
  131.  
  132.     update();
  133. }
  134.